import axios from 'axios';
// import qs from 'qs'
import store from '../store/index'       //已经设置路径别名，否则相对路径引用
import { ElMessage, ElLoading } from "element-plus";
import { useRouter } from 'vue-router';
let loading = null

// export const baseUrl = 'www.demoyyss.com';
// new.bcahz.com
// export const baseUrl = '192.168.0.5';
// export const baseUrl = 'demom.bcahz.com';
export const baseUrl = 'new.bcahz.com';
const service = axios.create({
    timeout: 50000,                                   //超时时间
    baseURL: "http://"+baseUrl+"/",             // 我们在请求接口的时候就不用写前面 会自动我们补全
    // transformRequest: data => qs.stringify(data)    //post请求参数处理,防止post请求跨域
})
// http request 拦截器
service.interceptors.request.use(config => {
    loading = ElLoading.service({
        lock: true,
        text: 'Loading',
        background: 'rgba(0, 0, 0, 0.7)',
    })
    //如果存在jwt，则将jwt添加到每次请求之中..
    if (window.localStorage.getItem("Token")) {
        config.params = {
            ...config.params,
            token: window.localStorage.getItem("Token")
        }
    }

    return config
}, err => {
    return err
})

// http response 拦截器
// const that = this
service.interceptors.response.use(response => {
    loading.close()
    return response
    //接收返回数据..
    // const res = response.data
    //判断返回数据是否存在状态code和错误提示信息..

    //判断状态code是否为指定数值(200)..
    // if (res.code != 200) {
    //     return showMessage(res.msg)
    // }
    // return res
}, err => {
    if ( err.message == 'Request failed with status code 401'){
        window.localStorage.removeItem('token');
        window.location.href="http://"+baseUrl+"/#"+'/login/'
        // window.location.href="http://localhost:8080/#/"+'login/'
        // console.log(window.localStorage.getItem('token'),'tokena')

    } 
    loading.close()
    return showMessage(err.message)
})


//封装错误提示信息..
function showMessage(message, type = "error", showClose = true) {
    ElMessage({
        showClose,          //是否显示X
        message,         //错误提示信息
        type,        //显示类型
        duration: 3 * 1000    //展示时间
    })
    return Promise.reject()
}

export default service